package com.power.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.power.entity.SystemProcessLog;
import com.power.service.SystemProcessLogService;
import com.power.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * ClassName:SystemProcessLogServiceImpl
 * Package:com.abrs.service.impl
 * Description:
 *
 * @date:2019/12/11 14:24
 * @author:zhangqiang
 */
@Service
@Slf4j
public class SystemProcessLogServiceImpl implements SystemProcessLogService {

    @Resource(name = "primaryRedisUtil")
    private RedisUtil redisUtil;

    @Override
    public void addSystemProcessLog(String procName, String procModule, Date startTime, Date endTime, Integer inputRow, Integer outputRow, String success, String procStatus) {
        //1.封装当前系统进程日志数据
        SystemProcessLog systemProcessLog = new SystemProcessLog();
        systemProcessLog.setProcName(procName);
        systemProcessLog.setProcModule(procModule);
        systemProcessLog.setStartTime(startTime);
        systemProcessLog.setEndTime(endTime);
        systemProcessLog.setInputRow(inputRow);
        systemProcessLog.setOutputRow(outputRow);
        //传入的success值有两种：Y或者W
        systemProcessLog.setSuccess(success);
        systemProcessLog.setProcStatus(procStatus);

        redisUtil.sAdd("SystemProcessNameSet", procName);
        //2.从redis中查询对应系统进程的日志数据
        String cacheProcessLog = (String) redisUtil.get("SystemProcessLog:" + procName, 0);
        if (StringUtils.isEmpty(cacheProcessLog)) {
            //cacheProcessLog为空表示第一次往缓存中设值
            //缓存系统进程的实时数据
            systemProcessLog.setTodayInputRow(inputRow);
            systemProcessLog.setTodayOutputRow(outputRow);
            systemProcessLog.setInputRowTotal(Long.valueOf(inputRow));
            systemProcessLog.setOutputRowTotal(Long.valueOf(outputRow));
            systemProcessLog.setTotalRunTime(endTime.getTime() - startTime.getTime());
            if (StringUtils.equalsIgnoreCase("W", success)) {
                systemProcessLog.setWarnNumber(inputRow - outputRow);
                systemProcessLog.setVersion(1);
            }
            redisUtil.set("SystemProcessLog:" + procName, JSONObject.toJSONString(systemProcessLog), 0);
        } else {
            //3.redis缓存中可以查询到对应系统进程的日志数据
            SystemProcessLog parseCacheLog = JSON.parseObject(cacheProcessLog, SystemProcessLog.class);
            systemProcessLog.setTodayInputRow(parseCacheLog.getTodayInputRow() + inputRow);
            systemProcessLog.setTodayOutputRow(parseCacheLog.getTodayOutputRow() + outputRow);
            systemProcessLog.setInputRowTotal(parseCacheLog.getInputRowTotal() + inputRow);
            systemProcessLog.setOutputRowTotal(parseCacheLog.getOutputRowTotal() + outputRow);
            systemProcessLog.setTotalRunTime(parseCacheLog.getTotalRunTime() + endTime.getTime() - startTime.getTime());
            //判断是否需要更新今日警告数据量
//            if (parseCacheLog.getWarnNumber() != null && parseCacheLog.getWarnNumber() > 0) {
////                if (StringUtils.equalsIgnoreCase("W", success)) {
////                    systemProcessLog.setWarnNumber(parseCacheLog.getWarnNumber() + inputRow);
////                } else {
////                    systemProcessLog.setWarnNumber(parseCacheLog.getWarnNumber());
////                }
////            }
            if (StringUtils.equalsIgnoreCase("W", success)) {
                //设置警告数量
                if (parseCacheLog.getWarnNumber() != null) {
                    systemProcessLog.setWarnNumber(parseCacheLog.getWarnNumber() + inputRow - outputRow);
                } else {
                    systemProcessLog.setWarnNumber(inputRow - outputRow);
                }
                //设置警告版本号
                if (parseCacheLog.getVersion() != null){
                    systemProcessLog.setVersion(parseCacheLog.getVersion()+1);
                    if(systemProcessLog.getVersion()>=3){
                        systemProcessLog.setSuccess("N");
                        systemProcessLog.setProcStatus("失败");
                    }
                }else {
                    systemProcessLog.setWarnNumber(1);
                }
            } else {
                //传入的success值为Y
                //设置警告数量
                if (parseCacheLog.getWarnNumber() != null) {
                    systemProcessLog.setWarnNumber(parseCacheLog.getWarnNumber());
                }
                //清空版本号至0
                systemProcessLog.setVersion(0);
            }
            redisUtil.set("SystemProcessLog:" + procName, JSONObject.toJSONString(systemProcessLog), 0);
        }
    }
}
